package com.mysoft.gfyx.clue.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mysoft.framework.common.user.User;
import com.mysoft.framework.common.util.MappingUtil;
import com.mysoft.framework.core.function.authorization.AuthenticationUser;
import com.mysoft.framework.service.AppService;
import com.mysoft.gfyx.clue.dao.ClueDao;
import com.mysoft.gfyx.clue.dao.CluefollowrecordDao;
import com.mysoft.gfyx.clue.dao.dto.ClueFollowRecordsDto;
import com.mysoft.gfyx.clue.dao.entity.Clue;
import com.mysoft.gfyx.clue.dao.entity.Cluefollowrecord;
import com.mysoft.gfyx.clue.service.CluefollowrecordService;
import com.mysoft.gfyx.clue.service.dto.ClueDto;
import com.mysoft.gfyx.clue.service.dto.ClueFollowRecordDto;
import com.mysoft.sdk.context.core.ThreadContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 描述：Cluefollowrecord 业务代码
 *
 * @author JavaSdk generator
 * @date 2022-07-07
 */
@Service
public class CluefollowrecordServiceImp extends AppService implements CluefollowrecordService {
    @Autowired
    private CluefollowrecordDao cluefollowrecordDao;

    @Autowired
    private ClueDao clueDao;

    public boolean addClueFollowRecord(ClueFollowRecordDto dto) {
        User authenticationUser = ThreadContextHolder.getInstance().get(AuthenticationUser.class);

        Cluefollowrecord cluefollowrecord = MappingUtil.getSimpleMapperFacade().map(dto, Cluefollowrecord.class);
        cluefollowrecord.setFollowerGUID(authenticationUser.getUserId());
        cluefollowrecord.setFollowerName(authenticationUser.getUserName());
        cluefollowrecordDao.insert(cluefollowrecord);

        // 更新最新跟进时间
        ClueDto clueDto = new ClueDto();
        clueDto.setClueGUID(cluefollowrecord.getClueGUID());
        clueDto.setFollowTime(cluefollowrecord.getCreatedTime());
        clueDto.setFollowerName(cluefollowrecord.getFollowerName());
        Clue clue = MappingUtil.getSimpleMapperFacade().map(clueDto, Clue.class);
        clueDao.updateById(clue);

        return true;
    }

    public List<ClueFollowRecordsDto> getClueFollowRecordsByClueId(UUID clueId) {
        return cluefollowrecordDao.queryClueFollowRecords(clueId);
    }
}
